
var LODOP; //声明为全局变量 
var expressRex="";//运单号正则表达式
var packformName="";
var printName="";//打印机名称
var status="";

function changeStatus(item){
	$("#firstExpressNumber").val('');
	status=item;
	if("packForm"==item){
		 packFormPrinter("packFormStylePrint");
	}else if("express"==item){
		 packFormPrinter("expressPrint");
	}else if("ebill"==item){
		 packFormPrinter("ebillPrint");
	}
}

function printExpress(taskName,printerName,content,width,height,rankno,expressNo) {
	LODOP.PRINT_INIT(taskName);
	LODOP.SET_PRINT_PAGESIZE(1,width,height,""); 
	LODOP.ADD_PRINT_TEXT(20,20,300,20,rankno);
	LODOP.ADD_PRINT_BARCODE(10, 200, 180, 40, "128B", expressNo);
	eval(content);
	if (LODOP.SET_PRINTER_INDEXA(printerName)) {
		LODOP.PRINT();
	}
}

function verify(content) {
	var result =content.match(expressRex);
	if(result==null){
	  return false;
	}
	return true;
}


function initLodop() {
	LODOP = getLodop(document.getElementById('LODOP_OB'), document
			.getElementById('LODOP_EM'));
}



function getBodyContentString(content) {
	content = content.replace(/<[^>]*?>(.*?)/gi, '='); //删除左部
	content = content.replace(/(.*?)<\/[^>]*?>/gi, '='); //删除右部
	var bodyContents = content.split("=");
	return bodyContents;
}


//整除
function Div(exp1, exp2){
	var n1 = Math.round(exp1); //四舍五入
	var n2 = Math.round(exp2); //四舍五入
	var rslt = n1 / n2; //除
	if (rslt >= 0){
	    rslt = Math.floor(rslt); //返回值为小于等于其数值参数的最大整数值。
	}else {
	    rslt = Math.ceil(rslt); //返回值为大于等于其数字参数的最小整数。
	}
	return rslt;
}


function pageAjaxPrinter(){
       
   if("express"==status){
        var firstExpressNumber = $("#firstExpressNumber").val();//运单号
	    var expressStylesId = $("#expressStylesId").val();//快递单样式
	
	    printName=$("#expressPrint").val();
        if(!verify(firstExpressNumber)){
           alert("请输入正确的订单号！");
           return;
        }
       ajaxPrintPackExpressOrdenos(curOrderno,firstExpressNumber,expressStylesId);
   }else if("packForm"==status){
	   printName= $("#packFormStylePrint").val();
   	   var packformStylesId = $("#packformStylesId").val();//随箱单样式
       ajaxPrintPackFormOrdenos(curOrderno,packformStylesId);
   }else if("ebill"==status){
	   printName= $("#ebillPrint").val();
	   var expressStylesId = $("#expressStylesId").val();//快递单样式
	   ajaxPrintEBillExpress(curOrderno,expressStylesId);
   }

}


function ajaxPrintPackFormOrdenos(ordenos,packformStyleId){
showBg();
$.ajax({
		type : 'post',
		url : '${ctx}/print/ajaxPrintPackFormOrdenos',
		data : 'ordenos=' + ordenos+'&packformStyleId='+packformStyleId,
		dataType : 'json',
		success : function(data) {
			 if (data.status == "1") {
				initLodop();
				$.each(data.content, function(i) {
					 printStyle(packformName,data.content[i].taskName,printName,data.content[i].content,data.content[i].ordeno,data.content[i].expressNo,data.content[i].taskName);
				});
			    $("#message").html("<span class=\"label label-success\">打印机加载完毕</span>");
			      closeBg();
			} else if (data.status == "-3") {
		    	closeBg();
				alert("请输入正确的运单号！");
			} else if (data.status == "0") {
			    closeBg();
				alert("加载错误！");
			}

		},

		error : function() {
            alert("服务器异常");
		}
	});
}

function ajaxPrintEBillExpress(ordenos,expressId){
	  showBg();
	$.ajax({
			type : 'post',
			url : '${ctx}/print/ajaxPrintEBillExpress',
			data : 'ordenos=' + ordenos+'&expressId='+expressId+'&taskCode='+taskCode,
			dataType : 'json',
			success : function(data) {
				 if (data.status == "1") {
					initLodop();
					$.each(data.content, function(i) {
						
						    var str=data.content[i].content;
						    str=str.replace(/\r\n/g,"");
						    printMailExpress(data.content[i].taskName,printName,str,data.width,data.height,data.content[i].expressNo);
						
					});
				    $("#message").html("<span class=\"label label-success\">打印机加载完毕</span>");
				       closeBg();
				} else if (data.status == "-3") {
			    	closeBg();
					alert("请输入正确的运单号！");
				} else if (data.status == "0") {
				    closeBg();
					alert("加载错误！");
				}

			},

			error : function() {

			}
		});
	}

function printMailExpress(taskName,printName,content,width,height,expressNo){
	LODOP.PRINT_INIT(taskName);
	//LODOP.ADD_PRINT_TEXT(10, 10, 200, 20, rankno);
	LODOP.SET_PRINT_PAGESIZE(1,width,height,""); 
	var strCenterStyle = "<style/>table {width:100%}</style>";
	LODOP.ADD_PRINT_HTM(-10, 12, "100%", "100%", strCenterStyle + content);
	LODOP.ADD_PRINT_BARCODE(27,43,220,40,"",expressNo);
	//LODOP.SET_SHOW_MODE("BKIMG_PRINT", 1);
	//LODOP.PREVIEW();
	if (LODOP.SET_PRINTER_INDEXA(printName)) {
		LODOP.PRINT();
	}
	
}

function ajaxPrintPackExpressOrdenos(ordenos,firstExpress,expressId){
  showBg();
$.ajax({
		type : 'post',
		url : '${ctx}/print/ajaxPrintPackExpressOrdenos',
		data : 'ordenos=' + ordenos+'&firstExpress='+firstExpress+'&expressId='+expressId,
		dataType : 'json',
		success : function(data) {
			 if (data.status == "1") {
				initLodop();
				$.each(data.content, function(i) {
					
					    var str=data.content[i].content;
					    str=str.replace(/\r\n/g,"");
					printExpress(data.content[i].taskName,printName,str,data.width,data.height,data.content[i].taskName,data.content[i].expressNo);
					
				});
			    $("#message").html("<span class=\"label label-success\">打印机加载完毕</span>");
			       closeBg();
			} else if (data.status == "-3") {
		    	closeBg();
				alert("请输入正确的运单号！");
			} else if (data.status == "0") {
			    closeBg();
				alert("加载错误！");
			}

		},

		error : function() {

		}
	});
}

function printStyle(indentfy,taskName, printerName, printContent,ordeno, expressNo,rankno){
  if("OSA-A5"==indentfy){
     printFivePackStyle(taskName,printerName,printContent,ordeno,expressNo,rankno);
  }else if("OSA-A4"==indentfy){
     printFourPackStyle(taskName,printerName,printContent,ordeno,expressNo,rankno);
  }else if("RY1_A5"==indentfy){
     printFivePackStyleRy(taskName,printerName,printContent,ordeno,expressNo,rankno);
  }else if("9love_A5"==indentfy){
     printFivePackStyleLove(taskName,printerName,printContent,ordeno,expressNo,rankno);
  }else if("HS1-A5"==indentfy){
	 printFivePackStyleHS1(taskName, printerName, printContent, ordeno,expressNo,rankno);
  }else{
     printFivePackStyle(taskName,printerName,printContent,ordeno,expressNo,rankno);
  }

}

function printFivePackStyleHS1(taskName, printerName, printContent, ordeno,expressNo,
		rankno) {
	LODOP.PRINT_INIT(taskName);
	LODOP.ADD_PRINT_TEXT(10, 10, 200, 20, rankno);

	var strCenterStyle = "<style/>table {width:100%}</style>";
	LODOP.ADD_PRINT_HTM(30, 0, "100%", "100%", strCenterStyle
			+ printContent);
	LODOP.ADD_PRINT_BARCODE(10, 50, 380, 40, "128B", ordeno);
	LODOP.ADD_PRINT_BARCODE(650, 280, 200, 20, "128B", expressNo);
	if (LODOP.SET_PRINTER_INDEXA(printerName)) {
		LODOP.PRINT();
	}
}

function printFivePackStyleLove(taskName, printerName, printContent,oraden, expressNo,rankno) {
	LODOP.PRINT_INIT(taskName);
	LODOP.ADD_PRINT_TEXT(20,20,300,10,rankno);
	
	LODOP.ADD_PRINT_BARCODE(40, 20, 180, 40, "128B", oraden);
	LODOP.ADD_PRINT_BARCODE(40, 250, 180, 40, "128B", expressNo);
	var strCenterStyle = "<style/>table {width:100%}</style>";
	LODOP.ADD_PRINT_HTM(80, 0, "100%", "100%", strCenterStyle
			+ printContent);
	if (LODOP.SET_PRINTER_INDEXA(printerName)) {
		LODOP.PRINT();
	}
}

function printFivePackStyleRy(taskName, printerName, printContent,ordeno, expressNo,rankno) {
	LODOP.PRINT_INIT(taskName);
	LODOP.ADD_PRINT_TEXT(20,20,300,10,rankno);

	var strCenterStyle = "<style/>table {width:100%}</style>";
	LODOP.ADD_PRINT_HTM(80, 0, "100%", "100%", strCenterStyle
			+ printContent);
    LODOP.ADD_PRINT_BARCODE(40, 10, 180, 40, "128B", ordeno);
	LODOP.ADD_PRINT_BARCODE(40, 250, 180, 40, "128B", expressNo);
	if (LODOP.SET_PRINTER_INDEXA(printerName)) {
		LODOP.PRINT();
	}
}

function printFivePackStyle(taskName, printerName, printContent,ordeno, expressNo,rankno) {
	LODOP.PRINT_INIT(taskName);
	LODOP.ADD_PRINT_TEXT(20,20,300,10,rankno);
	var strCenterStyle = "<style/>table {width:100%}</style>";
	LODOP.ADD_PRINT_HTM(115, 0, "100%", "100%", strCenterStyle+ printContent);
    LODOP.ADD_PRINT_BARCODE(40, 250, 350, 40, "128B", ordeno);
    if(expressNo){
    	LODOP.ADD_PRINT_BARCODE(500, 280, 200, 20, "128B", expressNo);
    }
	if (LODOP.SET_PRINTER_INDEXA(printerName)) {
		LODOP.PRINT();
	}
}

function printFourPackStyle(taskName, printerName, printContent,oraden, expressNo,rankno) {
	LODOP.PRINT_INIT(taskName);
	LODOP.ADD_PRINT_TEXT(20,20,300,10,rankno);
	
	LODOP.ADD_PRINT_BARCODE(40, 200, 300, 50, "128B", oraden);
	LODOP.ADD_PRINT_BARCODE(40, 830, 200, 40, "128B", expressNo);
	var strCenterStyle = "<style/>table {width:100%}</style>";
	LODOP.ADD_PRINT_HTM(80, 0, "100%", "100%", strCenterStyle
			+ printContent);
	if (LODOP.SET_PRINTER_INDEXA(printerName)) {
		LODOP.PRINT();
	}
}

function getAjaxExpress() {
    var expressStylesId = $("#expressStylesId").val();
		$.ajax({
			type : 'post',
			url : '${ctx}/packformStyle/getAjaxExpress',
			data : 'expressStylesId=' + expressStylesId,
			dataType : 'json',
			success : function(data) {
				if (data.status == "1") {
					expressRex=data.content.logisticsCompany.regMailNo;
				}  else if (data.status == "0") {
					alert("获取运单号正则表达式错误！");
				}
			},

			error : function() {

			}
		});
	}
	
	function getAjaxPackformStyle() {
		var packformStylesId = $("#packformStylesId").val();
		if (packformStylesId == "" || packformStylesId == null) {
			alert("还没有这个随箱单");
			return;
		}
		$.ajax({
			type : 'post',
			url : '${ctx}/packformStyle/getAjaxPackformStyle',
			data : 'packformStylesId=' + $("#packformStylesId").val(),
			dataType : 'json',
			success : function(data) {
				if (data.status == "1") {

					packformName = data.name;
					//packStyleType = data.type;
				
				} else if (data.status == "0") {
					alert("获取随箱单信息错误！");
				}

			},
			error : function() {
				alert("服务器异常");
			}
		});
	}
